round


描述

就像字面意思那样round():将一个小数向上或向下转换到最近的整数。在这种情况下,如果提供的数字正好是整数的一半(例如,1.5,17.5,-2.5等等)数字将取得<b2>偶数</b2>,例如,2.5将取最近的偶数,而3.5将取最近的偶数为4。这种舍入叫<i3>四舍五入取偶(bankers rounding)</i3> 。在需要大量迭代或使用浮点数计算时,这种四舍五入取偶比传统的四舍五入(即大于等于5则向上,小于5则向下)在统计上更加好用。

就是说要是某数的小数部分是0.5,那么它将取整到最近的偶数 。例如,23.5和24.5变为24,-23.5和-24.5都变成-24。This method treats positive and negative values symmetrically, so is therefore free of sign bias, and, more importantly, for reasonable distributions of values, the expected (average) value of the rounded numbers is the same as that of the original numbers.


语法:

round(n);


参数 描述
.n The number to round.


返回:

整数


例如:

score += round(hp / 5);

The above code will add a rounded integer onto the score value.